20 Minute Methods: Learn 30 JavaScript Methods in 12 days by Rodgers Travis
Author:Rodgers, Travis [Rodgers, Travis]
Language: eng
Format: mobi
Publisher: Travis Media
Published: 2019-01-12T16:00:00+00:00
Day 8: Math.round(), Math.ceil(), Math.floor(), Math.max(), Math.min()
Today we will finish up our numbers portion of the course by looking at Math.
What is this Math you speak about?
Math is a built-in JavaScript object that allows you to perform mathematical tasks on numbers.
Here are five simple Math methods that you will use often:
Math.round(); Math.ceil(); Math.floor(); Math.max(); Math.min();
In fact, you can probably tell what they do by just reading them.
Let's dig now into this brief lesson:
Math.round();
The Math.round() method returns the value of a number rounded to the nearest integer.
Open up a browser and the console (if you are viewing this in a browser, just open up the console under this email). Then type a variable such as:
var number = 12.8;
Hit Enter.
Now lets round:
Enter Math.round(number);
And yep, you get 13.
You can also just enter the number as the argument and get the same outcome like:
Math.round(12.8); <----13
Math.round(12.3); <----12
Now try these numbers out and get a feel for how it rounds to the nearest integer:
12456.33211
192.88111
Great!
Math.ceil(); and Math.floor();
Now for these two just remember this:
The ceiling is up and the floor is down.
Math.ceil() returns a value rounded up to its nearest integer.
Math.floor() returns a value rounded down to its nearest integer.
That's it!
Math.ceil(4.1); <---- 5
Math.floor(4.9); <---- 4
Simple enough.
Math.min() and Math.max()
The min() and max() methods do just what they suggest: They return either the lowest number of the numbers passed to it (min) or the largest number of the numbers passed to it (max).
Math.min(4, 2, 9); <---- will return 2
Math.max(4, 2, 9); <---- will return 9;
Let's try another:
var array = [3, 2, 5.4, 10, 3.2, 11, 20, 1, 0];
Now enter Math.min(array);
What did you get? 0?
Nope, you got NAN.
Why. Well, let's look at the parameters of this method.
Math.min([value1[, value2[, ...]]])
Look at those three dots. With ES2015, we can use this to pass an array. It is called a spread operator. This actually causes the array values to be expanded, or "spread", into the function's arguments.
Try it out:
Math.min(...array);
Math.max(...array);
Great job. And that's all for today. Go and rest your brain because our last four lessons are going to be quite a bit tougher.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(27118)
Hello! Python by Anthony Briggs(25970)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(25307)
Kotlin in Action by Dmitry Jemerov(24413)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(23611)
Dependency Injection in .NET by Mark Seemann(23326)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(21963)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(20867)
Grails in Action by Glen Smith Peter Ledbrook(19883)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17081)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(16847)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(14472)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(12595)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11876)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10656)
Hit Refresh by Satya Nadella(9247)
The Kubernetes Operator Framework Book by Michael Dame(8593)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8449)
Robo-Advisor with Python by Aki Ranin(8392)